//index.js
//获取应用实例
const app = getApp();
var rate = 0;
var canvasWidth = 0;
var canvasHeight = 0;
const md = require('../../utils/md5.js');
const post = require('../../utils/post.js');
Page({
  data: {
    imgUrls: [
      'http://eplus.zokoo.com.cn/upload/wallet/lunbo1.jpg'
    ],
    citys: '厦门市',
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    customItem: '全部',
    lineCanvasData: {
      canvasId: 'lineAreaCanvas',
    },
    model: 1,
    userinfo: wx.getStorageSync('userinfo')
  },
  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    console.log(app.systemInfo);
    var systemInfo = app.systemInfo;

    rate = systemInfo.screenWidth / 750;
    var updateData = {};
    canvasWidth = systemInfo.screenWidth - rate * 64 - 20;
    canvasHeight = rate * 306 + rate * 44 + rate * 34 + rate * 22;

    var yMax = 500;
    var yMin = 0;
    var xMax = 30;
    var xMin = 0;
    updateData['lineCanvasData.canvasWidth'] = canvasWidth;
    updateData['lineCanvasData.axisPadd'] = { left: 0, top: -10, right: rate * 5 };
    updateData['lineCanvasData.axisMargin'] = { bottom: rate * 34, left: rate * 26 };
    updateData['lineCanvasData.yAxis.fontSize'] = rate * 22;
    updateData['lineCanvasData.yAxis.fontColor'] = '#637280';
    updateData['lineCanvasData.yAxis.lineDash'] = { pattern: [0, 0], offset: 0 }
    updateData['lineCanvasData.yAxis.lineColor'] = '#F2F2F2';
    updateData['lineCanvasData.yAxis.lineWidth'] = rate * 2;
    updateData['lineCanvasData.yAxis.dataWidth'] = rate * 62;
    updateData['lineCanvasData.yAxis.isShow'] = true;
    updateData['lineCanvasData.yAxis.isDash'] = true;
    updateData['lineCanvasData.yAxis.minData'] = yMin;
    updateData['lineCanvasData.yAxis.maxData'] = yMax;
    updateData['lineCanvasData.yAxis.padd'] = rate * 306 / (yMax - yMin);

    updateData['lineCanvasData.xAxis.dataHeight'] = rate * 26;
    updateData['lineCanvasData.xAxis.fontSize'] = rate * 22;
    updateData['lineCanvasData.xAxis.fontColor'] = '#637280';
    updateData['lineCanvasData.xAxis.lineColor'] = '#DCE0E6';
    updateData['lineCanvasData.xAxis.lineWidth'] = rate * 2;
    updateData['lineCanvasData.xAxis.minData'] = xMin;
    updateData['lineCanvasData.xAxis.maxData'] = xMax;
    updateData['lineCanvasData.xAxis.padd'] = (canvasWidth - rate * 103) / (xMax - xMin);

    updateData['lineCanvasData.point'] = { size: rate * 4, isShow: false };
    updateData['lineCanvasData.canvasHeight'] = canvasHeight-20;

    // console.log(updateData)
    this.setData(updateData);
    this.getloopimg();
  },
  getloopimg: function () {
    var that = this;
    var datas = {};
    var datacall = function (res) {
      console.log(res);
      if (res.data.data.length != 0) {
        var imgurls=[];
        for(var i=0;i<res.data.data.length;i++){
          imgurls[i] = res.data.data[i].ImgUrl;
        }
        that.setData({
          imgUrls:imgurls
        })
      }
    }
    var res2 = post.getdata("/BO/BA_Banner/Query", datas, datacall);
  },
  onShow: function () {
    var that = this;
    var phone = wx.getStorageSync('phone');
    // var openId = (wx.getStorageSync('openId'));
    if (phone) {
      var apinameIsMeb = "/BO/BA_Member/Query";
      var timestamp = new Date().getTime();
      timestamp = parseInt(timestamp / 1000);
      var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
      wx.request({
        //后台接口地址
        url: app.server.url + apinameIsMeb,
        data: {
          sign: md5IsMeb,
          appid: app.server.appid,
          tmsp: timestamp,
          where: 'Tel =' + phone
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {

          // console.log(res);
          if (res.data.data.length != 0) {
            wx.setStorageSync('userinfo', res.data.data[0]);
            wx.setStorageSync('mebid', res.data.data[0].MebID);
            that.setData({
              userinfo: res.data.data[0],
              // star: res.data.data[0].StarLevel,
              // starnone: 5 - res.data.data[0].StarLevel,
            })
            that.getapply(res.data.data[0].MebID);
          } else {
            wx.redirectTo({
              url: '/pages/login/login',
            })
          }
        }
      })
      return;
    } else {
      wx.redirectTo({
        url: '/pages/login/login',
      })
    }
  },
  getapply: function (mebid) {
    var that = this;
    var depid = wx.getStorageSync('depid')
    if (mebid) {
      var where = 'MebID =' + mebid;
      if(depid){
        where+=" and DepID ="+depid;
      }
      var apinameIsMeb = "/BO/AP_ShopMembers/Query";
      var timestamp = new Date().getTime();
      timestamp = parseInt(timestamp / 1000);
      var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
      wx.request({
        //后台接口地址
        url: app.server.url + apinameIsMeb,
        data: {
          sign: md5IsMeb,
          appid: app.server.appid,
          tmsp: timestamp,
          where: where
        },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          console.log(res);
          if (res.data.data.length > 0) {
            wx.setStorageSync('newinfo', res.data.data[0]);
            wx.setStorageSync('depid', res.data.data[0].DepID);
            var star = res.data.data[0].StarLevel ? res.data.data[0].StarLevel : "";
            that.setData({
              newinfo: res.data.data[0],
              star: star,
              starnone: 5 - star,
            })  
            that.showzhexian(res.data.data[0].DepID);
          }else{
            wx.setStorageSync('newinfo', {});
            wx.setStorageSync('depid', '');
          }
        }
      })
      // return;
    }
  },
  showzhexian:function(depid){
    var that =this;
    // 获取返利详情
    var datas = {};
    datas.DepID = depid;
    datas.pageindex = 1;
    datas.pagesize = 31;
    datas.summary = 1;
    var datacall = function (res) {
      // console.log(res);
      if (res.data.data.length != 0) {
        var daymoney = res.data.data;
        var moneymin = daymoney[0].MnySum;//最小的营收
        var moneymax = daymoney[0].MnySum;//最大的营收
        for (var i = 0; i < daymoney.length; i++) {
          if (moneymax < daymoney[i].MnySum) {
            moneymax = daymoney[i].MnySum;
          }
          if (moneymin > daymoney[i].MnySum) {
            moneymin = daymoney[i].MnySum;
          }
        }
        that.setData({
          mmx:moneymax
        })
        console.log('最大为：' + moneymax + '最小为：' + moneymin)
        var systemInfo = app.systemInfo;
        rate = systemInfo.screenWidth / 750;
        var updateData = {};
        canvasWidth = systemInfo.screenWidth - rate * 64 - 25;
        canvasHeight = 100; //rate * 306 + rate * 44 + rate * 34 + rate * 22;
        var yMax = 500;
        var yMin = 0;
        var xMax = 30;
        // var xMin = 6500;
        var xMin = moneymin;
        if(xMin <=500){
          xMin=0;
        }else{
          xMin = Math.floor(xMin / 100) * 100 - Math.floor((Math.ceil((moneymax - moneymin) / 10)) / 100) * 100 ; 
          if(xMin < 0){
            xMin =0;
          }
        }
        var kedu = Math.ceil((moneymax - moneymin) /400);
        if(kedu <= 1){
          kedu =100;
        }else{
          kedu =kedu*100;
        }
        console.log(kedu);
        var dayx = [];
        var dayy = [];
        dayy[0] = { x: -1, y: 0, title: '日营收' };
        var iii = {};
        var bbb = {};
        for (var i = 0; i < daymoney.length; i++) {
          if (i == 6) {
            iii.title = "￥" + daymoney[i].MnySum;
            iii.isShowTitle = true;
          }
          iii.x = i;
          iii.y = (daymoney[i].MnySum - xMin)/(kedu/100);// * 1.8;
          dayx[i] = iii;
          bbb.x = i;
          bbb.y = 0;
          bbb.title = daymoney[i].AccDate.split(' ')[0];
          bbb.title = bbb.title.split('-')[1] + "." + bbb.title.split('-')[2];
          dayy[i + 1] = bbb;
          iii = {};
          bbb = {};
        }
       // console.log(dayx);//组装好的y上的数据
        //console.log(dayy);//组装好的x上的数据
        var series = [{
          // data: [
          //   { x: 0, y: (7400 - xMin) * 1.8 },
          //   { x: 1, y: (7000 - xMin) * 1.8 },
          //   { x: 2, y: (8000 - xMin) * 1.8 },
          //   { x: 3, y: (7590 - xMin) * 1.8 },
          //   { x: 4, y: (9500 - xMin) * 1.8 },
          //   { x: 5, y: (8500 - xMin) * 1.8 },
          //   { x: 6, y: (10800 - xMin) * 1.8, title: '￥10800', isShowTitle: true }
          // ]
          data: dayx
        }];
        var xAxisData = dayy;
        // var xAxisData = [
        //   { x: -1, y: 0, title: '日营收' },
        //   { x: 0, y: 0, title: '4.15' },
        //   { x: 1, y: 0, title: '4.16' },
        //   { x: 2, y: 0, title: '4.17' },
        //   { x: 3, y: 0, title: '4.18' },
        //   { x: 4, y: 0, title: '4.19' },
        //   { x: 5, y: 0, title: '4.20' },
        //   { x: 6, y: 0, title: '4.21' }
        // ];
       // console.log(xAxisData)
        var ysize = 100;
        var yAxisData = [
          { x: 0, y: 0, title: xMin },
          { x: 0, y: 1 * ysize, title: xMin + kedu },
          { x: 0, y: 2 * ysize, title: xMin + 2 * kedu },
          { x: 0, y: 3 * ysize, title: xMin + 3 * kedu },
          { x: 0, y: 4 * ysize, title: xMin + 4 * kedu },
          // { x: 0, y: 5 * ysize, title: xMin + 5 * kedu },
        ];
        yMax = 500;
        yMin = 0;
        xMax = 6;
        xMin = 0;
        updateData['lineCanvasData.xAxis.minData'] = xMin;
        updateData['lineCanvasData.xAxis.maxData'] = xMax;
        updateData['lineCanvasData.xAxis.padd'] = (canvasWidth - rate * 98) / (xMax - xMin);
        updateData['lineCanvasData.point'] = { size: rate * 4, isShow: false };
        updateData['lineCanvasData.yAxis.minData'] = yMin;
        updateData['lineCanvasData.yAxis.maxData'] = yMax;
        updateData['lineCanvasData.yAxis.padd'] = rate * 306 / (yMax - yMin);
        updateData['lineCanvasData.series'] = series;
        updateData['lineCanvasData.xAxis.data'] = xAxisData;
        updateData['lineCanvasData.yAxis.data'] = yAxisData;
        that.setData(updateData);
      }
    }
    var res2 = post.getdata("/Action/PK01/GetSale7DaySumRpt", datas, datacall);

  },
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("22222");
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  showshops: function () {
    wx.navigateTo({
      url: '/pages/shopdetail/shopdetail?id=' + this.data.newinfo.DepID,
    })
  },
  saoyisao: function () {
    wx.scanCode({
      success: (res) => {
        console.log(res.result);
        var datas = res.result.split('&');
        var memberarr = datas[0].split(':');
        console.log(memberarr[0] + "..." + memberarr[1]);
        if (memberarr[0] == 'mebid') {
          var that = this;
          // 获取时间戳
          var depid = datas[1].split(':')[1];
          var apinameIsMeb = "/Action/PK01/AddFriend";
          var timestamp = new Date().getTime();
          timestamp = parseInt(timestamp / 1000);
          var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
          wx.request({
            //后台接口地址
            url: app.server.url + apinameIsMeb,
            data: {
              sign: md5IsMeb,
              appid: app.server.appid,
              tmsp: timestamp,
              CMebID: wx.getStorageSync('mebid'),
              SMebID: memberarr[1],
              MyDepID: wx.getStorageSync('depid'),
              FrDepid: depid,
            },
            method: 'POST',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
              console.log(res);
              if (res.data.code == 0) {
                wx.navigateTo({
                  url: '/pages/linkman/linkman?id=' + res.data.data,
                })
              }
            }
          })
        } else {
          // if (memberarr[0] == 'payno'){
          // var payno =memberarr[1];
          // console.log(memberarr)
          // return;
          var head = memberarr[0].substring(0, 2) ;
          console.log(head);
          if(head != 'zk'){
            wx.navigateTo({
              url: '/pages/paytypec/paytypec?payno=' + memberarr[0],
            })
          }else{
            wx.navigateTo({
              url: '/pages/topay/topay?payno=' + memberarr[0],
            })
          }
          
          // }
        }
      }
    })
  },
  toreceive: function () {
    wx.navigateTo({
      url: '/pages/receivables/receivables',
    })
  },
  topay: function () {
    var that = this;
    var newinfo = that.data.newinfo;
    var userinfo = that.data.userinfo;
    if(!newinfo){
      wx.showModal({
        title: '',
        content: '您暂未绑定门店，无法使用付款功能',
      })
      return;
    }
    if (newinfo.MebID != newinfo.MasterMebID){
      wx.showModal({
        title: '',
        content: '非主账号无法使用付款功能',
      })
      return;
    }
    if (userinfo._BOState!=100){
      wx.showModal({
        title: '',
        content: '还未实名认证通过无法使用付款功能',
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/pay/pay',
    })
  },
  todaycount: function () {
    wx.navigateTo({
      url: '/pages/daycount/daycount',
    })
  },
  toshare: function () {
    wx.navigateTo({
      url: '/pages/share/share',
    })
  },
  toerror:function(){
    wx.showModal({
      title: '未实名认证',
      content: '您还未进行实名认证，无法使用收付款功能，请先实名认证',
    })
  }
})
